package com.shycloud.mido.music.scheduleTask;

import com.shycloud.mido.music.service.MisMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.time.LocalDateTime;

@Configuration
@EnableScheduling
public class DynamicScheduleTask implements SchedulingConfigurer {

	@Autowired
	private MisMusicService musicService;

	/**
	 * 执行定时任务
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

		taskRegistrar.addTriggerTask(

				() -> System.out.println("重置下载次数: " + LocalDateTime.now().toLocalTime()),

				triggerContext -> {
					//每月一日凌晨两点执行任务
					String cron = "0 0 2 1 * ?";
					musicService.updateDownLoadMp3Count();
					//返回执行周期(Date)
					return new CronTrigger(cron).nextExecutionTime(triggerContext);
				}
		);
	}

}
